<?php
/**
 * Created by laoyu.
 * User: hilaoyu@qq.com
 * Date: 2018/5/22
 * Time: 17:11
 */

namespace Hilaoyu\LaravelExtend\Providers;

use Hilaoyu\LaravelExtend\Services\Files\FileService;
use Hilaoyu\LaravelExtend\Supports\Cli;
use Hilaoyu\LaravelExtend\Supports\SubApp;
use Hilaoyu\LaravelPrivateApi\PrivateApiService;
use Hilaoyu\Utils\UtilCli;
use Hilaoyu\Utils\UtilSupport;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
use SoareCostin\FileVault\FileVaultServiceProvider;

class FileServiceProvider extends BaseServiceProvider
{

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        UtilSupport::tryQuiet(function (){
            app()->register(FileVaultServiceProvider::class);
        });

        $this->mergeConfigFrom($this->configPath('disk_temp.php'), 'filesystems.disks');
        $this->mergeConfigFrom($this->configPath('file_service.php'), 'file_service');



        SubApp::registerCommands(dirname(__DIR__).'/Services/Files/Console','Hilaoyu\\LaravelExtend\\Services\\Files\\Console');
    }

    public function boot()
    {
        //
        //dd(config('app.env',''),env('APP_ENV'));
        /*if(!config('file-vault.cipher','')){
            config()->set('file-vault.cipher','AES-256-CBC');
        }*/

        config()->set('file-vault.cipher','AES-256-CBC');
        $this->publishes([$this->configPath('file_service.php') => config_path('file_service.php')]);
        $this->registerRoutes();

        if(config('file_service.run_file_clean_temp',false) && !Cache::get('laravelExtendFilesCleanTempIsRunning_'.config('app.env',''))){
            Cache::put('laravelExtendFilesCleanTempIsRunning_'.config('app.env',''),true,Carbon::now()->addHours(1));
            $clean_temp_command = Cli::buildArtisanCommand('laravelExtend:filesCleanTemp');
            if(!UtilCli::hasRunning($clean_temp_command)){
                $output = null;
                $status = Cli::execute('laravelExtend:filesCleanTemp',$output,'',false);

                //dump($status,$output);
            }

        }

        //Cache::forget('laravelExtendFilesRunFileUseQueueIsRunning_'.config('app.env',''));
        if(config('file_service.run_file_usefor_queue',false) && !Cache::get('laravelExtendFilesRunFileUseQueueIsRunning_'.config('app.env',''))){
            Cache::put('laravelExtendFilesRunFileUseQueueIsRunning_'.config('app.env',''),true,Carbon::now()->addMinutes(10));
            $run_queue_command = Cli::buildArtisanCommand('laravelExtend:runFileUseQueue');
            if(!UtilCli::hasRunning($run_queue_command)){
                $output = null;
                $status = Cli::execute('laravelExtend:runFileUseQueue',$output,'',false);

                //dump($status,$output);
            }

        }



    }



    public function registerRoutes(){

        $upload_config = config('file_service.upload',[]);

        data_set($upload_config,'name','l-e-s.files');
        data_set($upload_config,'prefix','_les/files');
        SubApp::registerRoutes(dirname(__DIR__) . '/Services/Files/routes/api.upload.php','Hilaoyu\\LaravelExtend\\Services\\Files\\Controllers',$upload_config);


        if(!config('file_service.view.prefix')){
            config()->set('file_service.view.prefix','_les/files');
        }
        $view_config = config('file_service.view',[]);


        data_set($view_config,'name','l-e-s.files');
        SubApp::registerRoutes(dirname(__DIR__) . '/Services/Files/routes/web.view.php','Hilaoyu\\LaravelExtend\\Services\\Files\\Controllers',$view_config);


        foreach (config('filesystems.disks', '') as $disk_config){
            if(data_get($disk_config,'provide_service')){
                PrivateApiService::regiserRoutes(dirname(__DIR__) . '/Services/Files/private_service_routes','Hilaoyu\\LaravelExtend\\Services\\Files\\Controllers',[
                    'name'=>'l-e-s.files.service'
                ]);
                break;
            }
        }

        //Route::getRoutes()->refreshNameLookups();
        //Route::getRoutes()->refreshActionLookups();


        //dd(Route::getRoutes()->getRoutesByName());
    }

    protected function configPath(string $filename)
    {
        return dirname(__DIR__) . '/Services/Files/config/'.$filename;
    }
}
